<?php
/**
 *
 * Список items
 *
 * @author Chashin evgeniy
 *
 */
class Zen_Item extends Zen_List {

	/**
	 * Создание одной записи в таблице
	 * @param unknown_type $params
	 * @return int|boolean
	 */
	public function create($params) {
		$db = $this->getTable ()->getAdapter ();

		$params = $this->beforeCreate ( $params );

		//----------Добавляем запись в таблицу core--------


		$core = new Admin_Model_Core ();
		$coreparams = array ('create_date' => date ( 'Y-m-d H:i:s' ), 'update_date' => date ( 'Y-m-d H:i:s' ),'table'=>$this->getTableName());
		$user_id = Zen_User::getId ();
		if (Zen_User::getId ()) {
			$coreparams ['create_by'] = $user_id;
			$coreparams ['update_by'] = $user_id;
		}
		$cid = $core->create ( $coreparams );
		//==================================================
		if ($cid) {
			$params ['id'] = $cid;
			$id = $this->table->createRow ( $params )->save ();
			if ($id) {
				$this->afterCreate ( $id, $params );
				$this->afterChange ();
				$this->globalChange ();
			} else {
				$table = $this->getTableName ();
				throw new Exception ( "Can't create row in table " . $table );
			}
		} else {
			throw new Exception ( "Can't create row in table core" );
		}



		return $id;
	}

	/**
	 * Обновление параметров одной записи в таблице
	 * @param int $id
	 * @param array $params ассоциативный массив с параметрами записи
	 * @return boolean
	 */
	public function update($id, $params) {
		//Вызов события beforeUpdate
		$params = $this->beforeUpdate ( $id, $params );
		$row = $this->getTable ()->find ( $id )->current ();
		if ($row) {
			$db = $this->getTable ()->getAdapter ();

				$res = $row->setFromArray ( $params )->save ();
				if ($res > 0) {
					//----------Обновляем запись в таблице core--------
					$core = new Admin_Model_Core ();
					$coreparams = array ('update_date' => date ( 'Y-m-d H:i:s' ) );
					$user_id = Zen_User::getId ();
					if (Zen_User::getId ()) {
						$coreparams ['update_by'] = $user_id;
					}
					$core->update ( $id, $coreparams );
					//---------------------------------------------------
					$this->afterUpdate ( $id, $params );
					$this->afterChange ();
					$this->globalChange ();

				} else {
					$table = $this->getTableName ();
					throw new Exception ( "Can't update row in table " . $table );
				}
		} else
		$res = false;
		return $res;
	}
	/**
	 * Удаление записи в таблице
	 * @param int $id
	 * @return boolean
	 */
	public function delete($id) {
		$id = preg_replace ( '/\D/', '', $id );
		if ($id > 0) {
			$this->beforeDelete($id);
			$res = $this->table->delete ( "`id`='" . $id . "'");
			$this->afterDelete ( $id );
			if ($res > 0) {

				//----------Удаляем запись из таблицы core--------
				$core = new Admin_Model_Core ();
				$core->delete ( $id );
				//==================================================
				$this->afterChange ();
				$this->globalChange ();
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public function createMultiple($params) {
		throw new Exception ( "Method createMultiple aviable only in Zen_List " );
	}

}